package com.hos.utile;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by guom on 2018/10/21.
 */
public class DateUtile {
    public static String DATA_FORMAT_YMD = "yyyy-MM-dd";
    public static String DATA_FORMAT_YMD_1 = "yyyy/MM/dd";
    public static String DATA_FORMAT_YMD_HM = "yyyy-MM-dd HH:mm";
    public static String DATA_FORMAT_YMD_HM_1 = "yyyy/MM/dd HH:mm";
    public static String DATA_FORMAT_YMD_HMS = "yyyy-MM-dd HH:mm:ss";
    public static String DATA_FORMAT_YMD_HMS_1 = "yyyy/MM/dd HH:mm:ss";
    //
    public static String DATA_FORMAT_ZW_YMD = "yyyy年MM月dd日";

    public static String DATA_FORMAT_ZW_YMD_HMS = "yyyy年MM月dd日 HH:mm:ss";
    //
    public static String DATA_FORMAT_MD = "MM-dd";
    public static String DATA_FORMAT_MD_1 = "MM/dd";
    public static String DATA_FORMAT_ZW_MD = "MM月dd日";
    //
    public static String DATA_FORMAT_MD_HM = "MM-dd HH:mm";
    public static String DATA_FORMAT_MD_HM_1 = "MM月dd日 HH:mm";
    public static String DATA_FORMAT_MD_HM_2 = "MM/dd HH:mm";
    //
    public static String DATA_FORMAT_HM = "HH:mm";
    public static String DATA_FORMAT_HMS = "HH:mm:ss";
    //
    public static String getDateFormat(Date time, String type) {
        if (time == null) {
            time = new Date();
        }
        return getFormatDate(time, type);
    }

    //去秒
    public static String getDateFormatNoS(Date time, String type) {
        if (time == null) {
            time = new Date();
        }
        String timeFormat = getFormatDate(time, type);
        timeFormat = timeFormat.substring(0, (timeFormat.length() - 3));
        return timeFormat;
    }

    /**
     * 获取时间
     *
     * @param date
     * @param template
     * @return
     */
    private static String getFormatDate(Date date, String template) {
        SimpleDateFormat formatter = new SimpleDateFormat(template,
                Locale.SIMPLIFIED_CHINESE);
        String strDate = formatter.format(date);
        return strDate;
    }


    //支持yyyy-MM-dd HH:mm:ss格式，及被包含的格式
    public static Date stringToDate(String time, Date defout) {
        if (time==null || time.length() < 10) {
            return defout;
        }
        try {
            return stringToDate(time);
        } catch (Exception e) {
            return defout;
        }
    }

    //支持yyyy-MM-dd HH:mm:ss格式，及被包含的格式
    private static Date stringToDate(String time) {
        time.replace("年", "-");
        time.replace("月", "-");
        time.replace("日", "-");
        if (!time.contains("-")) {
            String y = time.substring(0, 4);
            String m = time.substring(4, 6);
            String d = time.substring(6, 8);
            time = y + "-" + m + "-" + d;
        }
        if (time==null || time.length() < 10) {
            return new Date();
        }
        if (time.length() == 10) {
            time += " 00:00:00";
        }
        if (time.length() == 16) {
            time += ":00";
        }

        if (time.length() != 19) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date daye = formatter.parse(time, pos);
        return daye;
    }

    /**
     * @param time(秒)
     * @return 00:00
     */
    public static String getTime(int time) {
        String f = "";
        String s = "";
        int f1 = time / 60;
        int s1 = time % 60;
        f = String.valueOf(f1);
        s = String.valueOf(s1);
        if (f1 <= 9) {
            f = "0" + f1;
        }
        if (s1 <= 9) {
            s = "0" + s1;
        }
        return f + ":" + s;
    }

}
